package com.cc.service.userAllType;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.List;
import java.util.stream.Collectors;

import com.cc.infrastructure.inter.userAllType.UserAllTypeInter;
import com.cc.web.vo.UserAllTypeVo;
import com.cc.infrastructure.db.UserAllTypeDb;
import com.cc.pojo.dto.UserAllTypeDto;
import com.cc.infrastructure.special.dto.UserAllTypeDo;

/**
 * @author C.c
 **/
@Service
public class UserAllTypeService {
  @Resource
  private UserAllTypeInter userAllTypeInter;

  /**
   * 分页
   *
   * @author C.c
   **/
  public Page<UserAllTypeVo> query(String name, int pageNum, int pageSize) {
    Page<UserAllTypeDo> userAllTypeDoDos = userAllTypeInter.query(name, pageNum, pageSize);
    List<UserAllTypeVo> collect = userAllTypeDoDos.stream().map(UserAllTypeVo::convertUserAllTypeVoByUserAllTypeDo).collect(Collectors.toList());
    return new PageImpl<>(collect, userAllTypeDoDos.getPageable(), userAllTypeDoDos.getTotalElements());
  }

  /**
   * 新增
   *
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean add(UserAllTypeDto userAllTypeDto) {
    UserAllTypeDb useralltypedb = UserAllTypeDto.convertUserAllTypeDbByUserAllTypeDto(userAllTypeDto);
    useralltypedb.setId(UUID.randomUUID().toString());
    userAllTypeInter.save(useralltypedb);
    return true;
  }

  /**
   * 删除
   *
   * @param id
   * @return boolean
   * @author C.c
   **/
  public boolean delete(String id) {
    userAllTypeInter.deleteById(id);
    return true;
  }

  /**
   * 修改
   *
   * @return boolean
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean edit(UserAllTypeDto userAllTypeDto) {
    UserAllTypeDb useralltypedb = UserAllTypeDto.convertUserAllTypeDbByUserAllTypeDto(userAllTypeDto);
    userAllTypeInter.save(useralltypedb);
    return true;
  }

  /**
   * 查询单个
   *
   * @param id 唯一标识
   * @author C.c
   **/
  public UserAllTypeVo view(String id) {
    UserAllTypeDb useralltypedb = userAllTypeInter.findById(id).orElse(null);
    return UserAllTypeVo.convertUserAllTypeVoByUserAllTypeDo(useralltypedb);
  }
}